package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Transactional
    public void addShopping(ShoppingCartDTO shoppingCartDTO) {
        //判断商品在购物车中是否存在
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        List<ShoppingCart> cartList = shoppingCartMapper.list(shoppingCart);

        //若存在，执行修改操作，使是商品数量加1
        if (cartList != null && !cartList.isEmpty()){
            ShoppingCart cart = cartList.get(0);
            cart.setNumber(cart.getNumber() + 1); //update shopping_cart set number = ? where id = ?
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //若不存在，将商品添加到购物车中

            //判断本次添加的是菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            if (dishId != null){
                //本次添加的是菜品
                Dish dish = dishMapper.getDishById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            }else {
                //本次添加的套餐
                Long setmealId = shoppingCart.getSetmealId();
                SetmealVO setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    /**
     * 根据userId查询购物车
     * @return
     */
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    public void delete() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中的一条数据
     * @param shoppingCartDTO
     */
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        //查询要删除的数据
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        List<ShoppingCart> cartList = shoppingCartMapper.list(shoppingCart);
        ShoppingCart shop = cartList.get(0);

        //若数量大于一，执行修改操作
        if (shop.getNumber() > 1){
            shop.setNumber(shop.getNumber() - 1);
            shoppingCartMapper.updateNumberById(shop);
        }else {
            //若数量等于一，执行删除操作
            shoppingCartMapper.deleteById(shop.getId());
        }
    }
}
